package com.allin.mall.user.service.impl;

import com.allin.mall.common.redis.annotation.DistributedLock;
import com.allin.mall.common.redis.service.RedisService;
import com.allin.mall.user.entity.Order;
import com.allin.mall.user.entity.User;
import com.allin.mall.user.mapper.OrderMapper;
import com.allin.mall.user.mapper.UserMapper;
import com.allin.mall.user.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

@Component
public class UserServiceImpl implements UserService, ApplicationContextAware {

    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private RedisService redisService;

    private ApplicationContext applicationContext;

    private int a = 0;

    public void testLock() {
        UserServiceImpl bean = this.applicationContext.getBean(this.getClass());
        Executor executor = Executors.newFixedThreadPool(10);

        for (int i = 0; i < 10000000; i++) {
            final int finalI = i;
            executor.execute(() -> bean.doTestLock(finalI));
        }
        System.out.println(a);
    }

    @DistributedLock(leaseTime = 10, keyPrefix = "ttttttttttttt", key = "#tt")
    public void doTestLock(int tt) {
        a ++;
    }


    @Override
    public User getUserById(Long id) {
        User result = new User();
        result.setId(id ++);
        result.setCode(id + "");
        return result;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
